﻿using BP.DA;
using BP.En;
using BP.Sys;
using BP.WF.Template.Frm;

namespace BP.Template.Printer
{
    /// <summary>
    /// 打印模板属性
    /// </summary>
    public class FrmPrintDBAttr : EntityMyPKAttr
    {
        /// <summary>
        /// 路径
        /// </summary>
        public const string Name = "Name";
        /// <summary>
        /// 路径
        /// </summary>
        public const string TempFilePath = "TempFilePath";
        /// <summary>
        /// NodeID
        /// </summary>
        public const string NodeID = "NodeID";
        /// <summary>
        /// 流程编号
        /// </summary>
        public const string FlowNo = "FlowNo";
        /// <summary>
        /// 字段名称
        /// </summary>
        public const string KeyOfEn = "KeyOfEn";
        /// <summary>
        /// 为生成单据使用
        /// </summary>
        public const string Idx = "Idx";
        /// <summary>
        /// 单据类型
        /// </summary>
        public const string TemplateFileModel = "TemplateFileModel";
        /// <summary>
        /// 是否生成PDF
        /// </summary>
        public const string PrintFileType = "PrintFileType";
        /// <summary>
        /// 二维码生成方式
        /// </summary>
        public const string QRModel = "QRModel";
        /// <summary>
        /// WorkID
        /// </summary>
        public const string WorkID = "WorkID";
        /// <summary>
        /// 表单的ID
        /// </summary>
        public const string FrmID = "FrmID";
    }
    /// <summary>
    /// 打印模板
    /// </summary>
    public class FrmPrintDB : EntityMyPK
    {
        #region  属性
        /// <summary>
        /// UI界面上的访问控制
        /// </summary>
        public override UAC HisUAC
        {
            get
            {
                UAC uac = new UAC();
                uac.OpenForSysAdmin();
                return uac;
            }
        }
        /// <summary>
        /// 编号
        /// </summary>
        public new string MyPK
        {
            get
            {
                string no = GetValStrByKey("MyPK");
                no = no.Replace("\n", "");
                no = no.Replace(" ", "");
                return no;
            }
            set
            {
                SetValByKey("MyPK", value);
                SetValByKey(FrmPrintDBAttr.TempFilePath, value);
            }
        }

        public string WorkID
        {
            get
            {
                return GetValStringByKey(FrmPrintDBAttr.WorkID);
            }
            set
            {
                SetValByKey(FrmPrintDBAttr.WorkID, value);
            }
        }

        public string FrmID
        {
            get
            {
                return GetValStringByKey(FrmPrintDBAttr.FrmID);
            }
            set
            {
                SetValByKey(FrmPrintDBAttr.FrmID, value);
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 打印模板
		/// </summary>
		public FrmPrintDB() { }
        /// <summary>
        /// 打印模板
        /// </summary>
        /// <param name="mypk">主键</param>
        public FrmPrintDB(string mypk) : base(mypk.Replace("\n", "").Trim())
        {
        }
        /// <summary>
        /// 获得单据文件流
        /// </summary>
        /// <param name="oid"></param>
        /// <returns></returns>
        public byte[] GenerFile()
        {
            byte[] bytes = DBAccess.GetByteFromDB("Sys_FrmPrintDB", "MyPK", this.MyPK, "DBFile");
            if (bytes != null)
                return bytes;

            //如果没有找到，就看看默认的文件是否有.
            string tempExcel = BP.Difference.SystemConfig.PathOfDataUser + "FrmVSTOTemplate/" + this.MyPK + ".xlsx";
            if (System.IO.File.Exists(tempExcel) == false)
                tempExcel = BP.Difference.SystemConfig.PathOfDataUser + "FrmVSTOTemplate/EmptyTemplate.xlsx";
            bytes = DataType.ConvertFileToByte(tempExcel);

            return bytes;
        }
        /// <summary>
        /// 获得单据文件流Word
        /// </summary>
        /// <param name="oid"></param>
        /// <returns></returns>
        public byte[] GenerWordFile()
        {
            byte[] bytes = DBAccess.GetByteFromDB("Sys_FrmPrintDB", "MyPK", this.MyPK, "DBFile");
            if (bytes != null)
                return bytes;

            //如果没有找到，就看看默认的文件是否有.
            string tempExcel = BP.Difference.SystemConfig.PathOfDataUser + "FrmVSTOTemplate/" + this.MyPK + ".docx";
            if (System.IO.File.Exists(tempExcel) == false)
                tempExcel = BP.Difference.SystemConfig.PathOfDataUser + "FrmVSTOTemplate/EmptyTemplate.docx";
            bytes = DataType.ConvertFileToByte(tempExcel);

            return bytes;
        }
        /// <summary>
        /// 重写基类方法
        /// </summary>
        public override Map EnMap
        {
            get
            {
                if (_enMap != null)
                    return _enMap;
                Map map = new Map("Sys_FrmPrintDB", "打印模板数据");
                map.IndexField = FrmPrintDBAttr.FrmID;
                map.AddMyPK();

                map.AddTBString("State", null, "状态", false, false, 0, 60, 60);

                map.AddTBString("FrmID", null, "表单ID", false, false, 0, 60, 60);
                map.AddTBString("FrmName", null, "表单名称", false, false, 0, 60, 60);
                map.AddTBString("WorkID", null, "实例ID", false, false, 0, 60, 60);
                map.AddTBString("FrmPrintTemplateID", null, "模板ID", false, false, 0, 60, 60);
                map.AddTBString("FrmPrintTemplateName", null, "模板名称", true, false, 0, 200, 100);
                map.AddTBString("RTFFileName", null, "文件名称", true, false, 0, 200, 100);
                map.AddTBString("RecNo", null, "打印人ID", false, false, 0, 60, 60);
                map.AddTBString("RecName", null, "打印人名称", false, false, 0, 60, 60);
                map.AddTBDateTime("RDT", null, "打印日期", false, false);
                map.AddTBString("FileExt", null, "文件后缀", false, false, 0, 60, 60);
                map.AddTBString("State", null, "状态", false, false, 0, 60, 60);
                //DBFile 存储二进制文件.

                _enMap = map;
                return _enMap;
            }
        }
        #endregion

        protected override bool beforeInsert()
        {
            if (DataType.IsNullOrEmpty(MyPK) == true)
                MyPK = DBAccess.GenerGUID();
            return base.beforeInsert();
        }
    }
    /// <summary>
    /// 打印模板s
    /// </summary>
    public class FrmPrintDBs : EntitiesMyPK
    {
        #region 构造
        /// <summary>
        /// 得到它的 Entity 
        /// </summary>
        public override Entity GetNewEntity
        {
            get
            {
                return new FrmPrintDB();
            }
        }
        /// <summary>
        /// 打印模板
        /// </summary>
        public FrmPrintDBs()
        {
        }
        public FrmPrintDBs(int nodeID)
        {
            Retrieve(FrmPrintDBAttr.NodeID, nodeID);
        }
        public FrmPrintDBs(string flowNo)
        {
            Retrieve(FrmPrintDBAttr.FlowNo, flowNo);
        }
        #endregion

        #region 为了适应自动翻译成java的需要,把实体转换成List.
        /// <summary>
        /// 转化成 java list,C#不能调用.
        /// </summary>
        /// <returns>List</returns>
        public System.Collections.Generic.IList<FrmPrintDB> ToJavaList()
        {
            return (System.Collections.Generic.IList<FrmPrintDB>)this;
        }
        /// <summary>
        /// 转化成list
        /// </summary>
        /// <returns>List</returns>
        public System.Collections.Generic.List<FrmPrintDB> Tolist()
        {
            System.Collections.Generic.List<FrmPrintDB> list = new System.Collections.Generic.List<FrmPrintDB>();
            for (int i = 0; i < Count; i++)
            {
                list.Add((FrmPrintDB)this[i]);
            }
            return list;
        }
        #endregion 为了适应自动翻译成java的需要,把实体转换成List.
    }

}
